<template>
  <div class="registerDetail">
    <!-- 单车详情 -->
    <div class="detail_header flex">
      <div class="detail_header_lt">
        <div class="carNo flex">
          {{ dataSource.taskName }}<i class="iconfont icon-right"></i>
        </div>
        <div class="hank flex">
          <div class="name">{{ dataSource.customerName }}</div>
          <div class="carName">{{ dataSource.carModelName }}</div>
        </div>
      </div>
      <div class="detail_header_rt">
        <a class="flex" :href="`tel:${dataSource.ownerPhone}`">
          <i class="iconfont icon-tel"></i>
          <div class="txt">联系车主</div>
        </a>
      </div>
    </div>
    <!-- 表单 -->
    <div class="formData">
      <van-form @submit="formOnSubmit">
        <van-field
          v-model="formData.followResult"
          name="跟进结果"
          label="跟进结果"
          input-align="right"
          placeholder="请选择"
          right-icon="arrow"
          @click="handleSelect('fixCenterId', 'followResult')"
        />
        <van-field
          v-model="formData.followType"
          name="跟进方式"
          label="跟进方式"
          input-align="right"
          placeholder="请选择"
          right-icon="arrow"
          @click="handleMoreSelect('empId', 'followType')"
        />
        <van-field
          v-model="formData.empName"
          name="跟进人员"
          label="跟进人员"
          input-align="right"
          placeholder="请选择"
          right-icon="arrow"
          @click="handleMoreSelect('empId', 'empName')"
        />

        <van-field
          readonly
          clickable
          name="跟进时间"
          :value="formData.followDate"
          input-align="right"
          label="跟进时间"
          placeholder="请选择时间"
          @click="handleSelectTime('followDate')"
          right-icon="arrow"
        />
        <van-field
          readonly
          clickable
          name="下次跟进时间"
          :value="formData.nextFollowDate"
          input-align="right"
          label="下次跟进时间"
          placeholder="请选择时间"
          @click="handleSelectTime('nextFollowDate')"
          right-icon="arrow"
        />

        <van-field
          v-model="formData.followNote"
          name="跟进备注"
          label="跟进备注"
          type="textarea"
          input-align="left"
          placeholder=""
        />

        <!-- 时间选择弹窗 -->
        <van-popup v-model="showTimerPicker" position="bottom">
          <van-datetime-picker
            type="datetime"
            @confirm="onTimeConfirm($event)"
            @cancel="showTimerPicker = false"
            :formatter="formatter"
          />
        </van-popup>

        <!-- 下拉选择弹窗 -->
        <van-popup v-model="showSelectPicker" position="bottom">
          <van-picker
            show-toolbar
            :columns="columns"
            @confirm="handleSelectConfirm"
            @cancel="showSelectPicker = false"
            value-key="name"
          />
        </van-popup>

        <div class="submitBtn" @click="formOnSubmit">确认</div>
      </van-form>
    </div>
  </div>
</template>

<script>
import {
  Search,
  Empty,
  Field,
  Form,
  Uploader,
  DatetimePicker,
  Tab,
  Tabs,
  ImagePreview,
} from "vant";
import taskApi from "@/api/taskApi";
import { getCookies } from "@/utils/util.js";
export default {
  name: "taskCenterEdit",
  components: {
    Search,
    Empty,
    Field,
    Form,
    Uploader,
    DatetimePicker,
    Tab,
    Tabs,
    ImagePreview,
  },
  data() {
    return {
      showTimerPicker: false,
      dataSource: {
        billId: "1", //单据id
        taskTypeName: "定期保养", //任务类型
        taskName: "闽D66666定期保养", //任务名称
        oneBillStatus: "未处理", //任务一级状态
        twoBillStatus: "未处理", //任务二级状态
        carModelName: "Ct5 2.0T", //车型
        customerName: "张三", //客户名称
        mobile: "187231231", //电话
        ownerName: "李四", //车主姓名
        ownerPhone: "187231231", //车主电话
      },
      formData: {
        followResult: "", //跟进结果，String，必填
        followType: "", //跟进类型,必填，String
        billId: "", //父ID,必填，String
        followDate: "", // 跟进时间，必填
        nextFollowDate: "", //下次跟进时间，必填
        followNote: "", //跟进备注，必填
        failed: "", //战败原因，跟进结果是战败时必填
        followSupple: "", //战败补充
        userId: "", //用户ID，必填，String
      },
      columns: [],
      showSelectPicker: false,
      detailId: "",
      userId: getCookies("userId"), //用户ID，必填，String
    };
  },

  mounted() {
    if (this.$route.query) {
      this.detailId = this.$route.query.billId;
      this.getDetail(this.detailId);
    }
  },
  methods: {
    getDetail(billId) {
      taskApi
        .getCustomerTaskById({ billId, userId: this.userId })
        .then((res) => {
          this.dataSource = res;
        });
    },

    // 选择下拉
    handleSelect(field) {
      this.showSelectPicker = true;
      if (field == "repair") {
        this.columns = [
          { name: "是", value: 1 },
          { name: "否", value: 2 },
        ];
      } else if (field == "outUser") {
        this.columns = [
          { name: "服务人员1", value: 1 },
          { name: "服务人员2", value: 2 },
        ];
      }
      this.field = field;
    },
    // 选择下拉确认
    handleSelectConfirm(value, index) {
      this.formData[this.field] = value.name;
      this.showSelectPicker = false;
    },

    onConfirm(val, field) {
      let year = val.getFullYear();
      let month = val.getMonth() + 1;
      let day = val.getDate();
      let hour = val.getHours();
      let minute = val.getMinutes();
      // let second = val.getSeconds()
      if (month >= 1 && month <= 9) {
        month = `0${month}`;
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`;
      }
      if (hour >= 0 && hour <= 9) {
        hour = `0${hour}`;
      }
      if (minute >= 0 && minute <= 9) {
        minute = `0${minute}`;
      }
      // if (second >= 0 && second <= 9) { second = `0${second}` }
      this.formData[field] = `${year}-${month}-${day} ${hour}:${minute}:00`;
      this.showTimerPicker = false;
    },

    formatter(type, value) {
      if (type === "year") {
        return `${value}年`;
      } else if (type === "month") {
        return `${value}月`;
      } else if (type === "day") {
        return `${value}日`;
      } else if (type === "hour") {
        return `${value}时`;
      } else if (type === "minute") {
        return `${value}分`;
      } else if (type === "second") {
        return `${value}秒`;
      }
      return value;
    },
    // 时间选择
    handleSelectTime(field) {
      this.timeField = field;
      this.showTimerPicker = true;
    },
    onTimeConfirm(val) {
      let year = val.getFullYear();
      let month = val.getMonth() + 1;
      let day = val.getDate();
      let hour = val.getHours();
      let minute = val.getMinutes();
      // let second = val.getSeconds()
      if (month >= 1 && month <= 9) {
        month = `0${month}`;
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`;
      }
      if (hour >= 0 && hour <= 9) {
        hour = `0${hour}`;
      }
      if (minute >= 0 && minute <= 9) {
        minute = `0${minute}`;
      }
      // if (second >= 0 && second <= 9) { second = `0${second}` }
      this.formData[
        this.timeField
      ] = `${year}-${month}-${day} ${hour}:${minute}:00`;
      this.showTimerPicker = false;
    },
    //维修预约
    applyRepair() {
      this.$router.push("/applyRepair");
    },
    // 获取list名称
    getFieldName(list, key, val) {
      if (list && list.length > 0) {
        let obj = list.filter((item) => {
          return item[key] == val;
        });
        if (obj.length > 0) {
          return obj[0].name;
        }
      }
      return null;
    },
    formOnSubmit() {},
  },
};
</script>

<style lang="less" scoped>
body {
  background: #f3f3f3;
}
.flex {
  display: flex;
  align-items: center;
}
.registerDetail {
  overflow: hidden;
  .detail_header {
    justify-content: space-between;
    margin: 0.2rem 0.2rem;
    border-radius: 5px;
    background: #fff;
    padding: 0.3rem;
    .detail_header_lt {
      .carNo {
        font-size: 0.32rem;
        font-weight: bold;
        margin-bottom: 0.2rem;
        .carStatus {
          border: 1px solid #35bc04;
          color: #35bc04;
          border-radius: 5px;
          padding: 0.1rem 0.1rem;
          font-size: 0.24rem;
          text-align: center;
          margin-left: 0.1rem;
        }
      }
      .hank {
        font-size: 0.28rem;
        color: #666;
        .name {
          border-right: 2px solid #ccc;
          padding-right: 0.1rem;
          margin-right: 0.1rem;
        }
      }
    }
    .detail_header_rt {
      justify-content: flex-start;
      .txt {
        font-size: 0.28rem;
        color: #161616;
      }
      .icon-tel {
        color: #fff;
        background: #35bc04;
        font-size: 0.32rem;
        padding: 0.1rem;
        border-radius: 50%;
        margin-right: 0.1rem;
      }
    }
  }
  .formData {
    background: #fff;
    overflow: hidden;
  }
}

.submitBtn {
  background: #409eff;
  color: #fff;
  width: 75%;
  margin: auto;
  text-align: center;
  padding: 0.25rem;
  border-radius: 5px;
  margin-top: 0.6rem;
  font-size: 0.3rem;
  margin-bottom: 0.5rem;
}
</style>

